package com.invengo.module_check.ui.fragment.bookdown;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.invengo.library_rfid.classify.AnalyzeImpl;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.databinding.FragmentBookDownBinding;
import com.invengo.module_check.ui.fragment.adapter.DepthPageTransformer;
import com.invengo.module_check.ui.fragment.bookdown.fragment.BookDownByBookFragment;
import com.invengo.module_check.ui.fragment.bookdown.fragment.BookDownByMultipleShelfFragment;
import com.invengo.module_check.ui.fragment.bookdown.fragment.BookDownBySingleShelfFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.KLog;

public class BookDownFragment extends BaseFragment<FragmentBookDownBinding, BaseViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_book_down;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }



    @Override
    public void initData() {
        super.initData();
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(new BookDownByBookFragment());
        fragmentList.add(new BookDownBySingleShelfFragment());
        fragmentList.add(new BookDownByMultipleShelfFragment());
        List<String> tabs = new ArrayList<>();
        tabs.add("按本下架");
        tabs.add("单层下架");
        tabs.add("多层下架");

        FragmentStatePagerAdapter adapter =new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                // 返回每个 Fragment 的标签文本
                switch (position) {
                    case 0:
                        return "按本下架";
                    case 1:
                        return "单层下架";
                    case 2:
                        return "多层下架";
                    default:
                        return "";
                }
            }
        };
        binding.viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                changeSub.onNext(position);

            }

            @Override
            public void onPageSelected(int position) {
                changeSub.onNext(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.viewpager.setOffscreenPageLimit(1);
        binding.viewpager.setAdapter(adapter);
        binding.tablayout.setupWithViewPager(binding.viewpager);

        changeSub
                .subscribeOn(Schedulers.io())
                .debounce(300, TimeUnit.MILLISECONDS)
                .delay(300, TimeUnit.MILLISECONDS)
                .switchMap((Function<Integer, ObservableSource<Integer>>) query -> Observable.just(query))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(integer -> {
                    fragmentList.get(integer).changeModel();
                });
    }

    PublishSubject<Integer> changeSub = PublishSubject.create();
}
